package com.portico.dept.solarch

import org.springframework.format.datetime.DateFormatter
import java.text.SimpleDateFormat

@grails.plugins.springsecurity.Secured(['ROLE_SA_USER','ROLE_SA_ADMIN'])
class ProjectDashboardController {

  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  static scaffold = true

  def index = {
    redirect(action: "list", params: params)
  }

  def list = {  ProjectDashboardCommand cmd ->

    flash.message =  cmd.startDate.toString() + "  to   " + cmd.endDate.toString()

    def projectStatusList  =  ProjectStatus.findAllByStatusDateBetween (cmd.startDate, cmd.endDate)
    def projectDashboardInstance = ProjectDashboard.findAll() ;
//    for ( ins in projectDashboardInstance) {
//      if (ins.showProjectDashboard != true ) projectDashboardInstance.remove(ins)
//    }

    withFormat {
      html {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render(view:'list', model:[projectDashboardInstanceList: projectDashboardInstance,
                projectStatusInstanceList: projectStatusList,
                projectDashboardInstanceTotal: ProjectDashboard.count()])
      }
      csv {
        String csvString = ProjectDashboard.CSV_HEADER + System.properties.getProperty("line.separator");
        projectDashboardInstance.each {
          if ( it.showProjectDashboard) csvString += it.toCSVString() + System.properties.getProperty("line.separator")
        }
        render(text: csvString, contentType: "text/csv", encoding: "UTF-8")
      }
    }
  }

  def create = {
    def projectDashboardInstance = new ProjectDashboard()
    projectDashboardInstance.properties = params
    return [projectDashboardInstance: projectDashboardInstance]
  }

  def remote_save = {
      def projectDashboardInstance = new ProjectDashboard(params)
      if (projectDashboardInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), projectDashboardInstance.id])}"
          render (text: 'Save Complete... ')
      }
      else {
        render(view: "create", model: [projectDashboardInstance: projectDashboardInstance])
      }

  }

  def save = {
    def projectDashboardInstance = new ProjectDashboard(params)
    if (projectDashboardInstance.save(flush: true)) {
      flash.message = "${message(code: 'default.created.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), projectDashboardInstance.id])}"
      redirect(action: "show", id: projectDashboardInstance.id)
    }
    else {
      render(view: "create", model: [projectDashboardInstance: projectDashboardInstance])
    }
  }

  def show = {
    def projectDashboardInstance = ProjectDashboard.get(params.id)
    if (!projectDashboardInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
      redirect(action: "list")
    }
    else {
      [projectDashboardInstance: projectDashboardInstance]
    }
  }

  def edit = {
    def projectDashboardInstance = ProjectDashboard.get(params.id)
    if (!projectDashboardInstance) {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
      redirect(action: "list")
    }
    else {
      return [projectDashboardInstance: projectDashboardInstance]
    }
  }

  def update = {
    def projectDashboardInstance = ProjectDashboard.get(params.id)
    if (projectDashboardInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (projectDashboardInstance.version > version) {

          projectDashboardInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'projectDashboard.label', default: 'ProjectDashboard')] as Object[], "Another user has updated this ProjectDashboard while you were editing")
          render(view: "edit", model: [projectDashboardInstance: projectDashboardInstance])
          return
        }
      }
      projectDashboardInstance.properties = params
      if (!projectDashboardInstance.hasErrors() && projectDashboardInstance.save(flush: true)) {
        flash.message = "${message(code: 'default.updated.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), projectDashboardInstance.id])}"
        redirect(action: "show", id: projectDashboardInstance.id)
      }
      else {
        render(view: "edit", model: [projectDashboardInstance: projectDashboardInstance])
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
      redirect(action: "list")
    }
  }

  def delete = {
    def projectDashboardInstance = ProjectDashboard.get(params.id)
    if (projectDashboardInstance) {
      try {
        projectDashboardInstance.delete(flush: true)
        flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'projectDashboard.label', default: 'ProjectDashboard'), params.id])}"
      redirect(action: "list")
    }
  }
}

class ProjectDashboardCommand {

  Date startDate = new Date() - 7 ;
  Date endDate = new Date();
}